package com.zhkj.txg.module.home.adapter

import android.view.View
import android.widget.ImageView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.zhkj.txg.R
import com.zhkj.txg.module.home.entity.MallShopEntity
import com.zhkj.txg.module.home.ui.ShopActivity
import com.zhkj.txg.utils.ImageUtils

class MallAdapter : BaseQuickAdapter<MallShopEntity, ShopViewHolder>(R.layout.item_mall),
    LoadMoreModule {

    init {
        addChildClickViewIds(R.id.viewShop)
        setOnItemChildClickListener { _, view, position ->
            val shop = data[position]
            when (view.id) {
                R.id.viewShop -> if (shop.id > 0) ShopActivity.open(context, shop.shopName, shop.id)
            }
        }
    }

    override fun convert(helper: ShopViewHolder, item: MallShopEntity) {
        val imageView = helper.setText(R.id.tvMallShopName, item.shopName)
            .getView<ImageView>(R.id.ivMallShop)
        ImageUtils.loadCircleImage(context, imageView, item.shopImg)
        helper.productAdapter.replaceData(item.goods)
    }

    override fun onItemViewHolderCreated(viewHolder: ShopViewHolder, viewType: Int) {
        with(viewHolder.getView<RecyclerView>(R.id.rvMall)) {
            hasFixedSize()
            layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
            adapter = viewHolder.productAdapter
        }
    }
}

class ShopViewHolder(view: View) : BaseViewHolder(view) {
    val productAdapter = MallProductAdapter()
}